<?php
namespace Operationtest\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Sql;

class StoreTable extends AbstractTableGateway
{
    protected $table ='store';
    
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Store());
        $this->initialize();
    }
    
    public function fetchAll()
    {
        $adapter = $this->adapter;
        $sql = new Sql($adapter);
        $select = $sql->select();
        $select->columns(array('idStore','name', 'groupmentCode'));
        $select->from($this->table);
        $select->order('name ASC');
        
        $selectString = $sql->getSqlStringForSqlObject($select);
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);   
        return $results;
    }
    
    public function fetchAllForRHData() {
        $adapter = $this->adapter;
        $sql = new Sql($adapter);
        $select = $sql->select();
        $select->from($this->table);
        $select->order('groupmentCode ASC');
        
        $selectString = $sql->getSqlStringForSqlObject($select);
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }
    
    public function getStore($id)
    {
        $id  = (int) $id;
        $rowset = $this->select(array('idStore' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
    
    public function getStorelist()
    {
        $adapter = $this->adapter;
        $sql = new Sql($adapter);
        $select = $sql->select();
        $select->columns(array(
            'value' => 'idStore', 
            'text' => 'name'
        ));
        $select->from($this->table);
        $select->order('name ASC');
        
        $selectString = $sql->getSqlStringForSqlObject($select);
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);   
        return $results;
    }
}
